class Player extends egret.Sprite {
	public constructor() {
		super();
		this.getPlayerAnimation();
	}

	private playerWalkAnimation: egret.MovieClip;

	private getPlayerAnimation() {
		let mcDataFactory: egret.MovieClipDataFactory = new egret.MovieClipDataFactory(RES.getRes("robotJump_json"), RES.getRes("robotJump_png"));
		let mc: egret.MovieClip = new egret.MovieClip(mcDataFactory.generateMovieClipData("robotJump"));

		mc.anchorOffsetX = mc.width / 2;
		mc.anchorOffsetY = mc.height;

		this.playerWalkAnimation = mc;
		this.addChild(this.playerWalkAnimation);

	}
	private lastX:number = 320;
	private lastY:number = 710;
	//0:往左跳，1：往右跳
	public playerJump(direction: number) {
		let preDis = direction===0?-1:1 
		let playerJump: egret.MovieClip = this.playerWalkAnimation;
		let lastDirection: number;
		this.lastX += 75*preDis;
		this.lastY += -90;
		if (lastDirection == direction) {
			
			//this.x = this.x+(75*preDis);
			//this.y = this.y-100;
			
		} else {
			if (direction == 0) {
				playerJump.scaleX = 1;
			} else {
				playerJump.scaleX = -1;
			}
			//playerJump.gotoAndPlay(1,1);
			// this.x = this.x+(75*preDis);
			// this.y = this.y-100;
			
		}
		playerJump.gotoAndPlay(1,1);
	
		egret.Tween.removeTweens(this);
		egret.Tween.get(this).to({x: this.lastX, y: this.lastY }, 260);
		lastDirection = direction;
	}
	

	//0:面向左，1：面向右
	public playerDirection(direction: number){
		if(direction == 0){
			this.playerWalkAnimation.scaleX = 1;
		}
		if(direction == 1){
			this.playerWalkAnimation.scaleX = -1;
		}
	}

	// 0:跳错 1:时间到
	public playerDied(diedNum:number){
		egret.log("playerDied===>",diedNum);
		if(diedNum == 0){
			this.playOver();
			//this.addEventListener(egret.Event.COMPLETE,this.playOver,this);
		}
		if(diedNum == 1){
			this.playOver();
		}
	}

	private playOver(){
		egret.log("over")
		//this.removeEventListener(egret.Event.COMPLETE,this.playOver,this);
		egret.Tween.get(this).to({y:this.y + 200},350).call(function(){
				this.parent.removeChild(this);});
	}

}